//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package main

import (
	"fmt"
	"net"
	"net/http"
	"os"

	"atomgit.com/openlinksaas/ssh-proxy/gen_code"
	"atomgit.com/openlinksaas/ssh-proxy/impl"
)

func main() {
	l, err := net.Listen("tcp4", "127.0.0.1:")
	if err != nil {
		os.Stderr.Write([]byte(err.Error()))
		return
	}
	host := l.Addr().String()
	token := impl.Init()
	fmt.Printf("listen=%s,token=%s\n", host, token)

	r := NewRouter(&ApiDef{host},
		gen_code.NewConnectionApiController(&impl.ConnectionApiImpl{}),
		gen_code.NewSftpApiController(&impl.SftpApiImpl{}),
		gen_code.NewPortforwardApiController(&impl.PortforwardApiImpl{}),
		gen_code.NewShellApiController(&impl.ShellApiImpl{}),
	)
	r.Methods("GET").PathPrefix("/").Name("swagger").Handler(&Web{})
	r.Methods("OPTIONS").PathPrefix("/").Name("options").Handler(&OptionsHandler{})
	s := &http.Server{
		Handler: r,
	}
	s.Serve(l)
}
